/**
 * 项目名：phms3
 * 日期：    2019-11-18 12:02:32
 * Copyright (c) 2015- HTSC-版权所有
 */

package com.joysuch.wwyt.core.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.joysuch.wwyt.core.enums.LoginUserStates;
import com.joysuch.wwyt.core.support.AutoSetCommonFieldListener;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLDeleteAll;
import org.hibernate.annotations.Where;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.util.LinkedHashSet;
import java.util.Set;

@EntityListeners({AuditingEntityListener.class, AutoSetCommonFieldListener.class})
@Entity
@Table(name = "WWYT_BASE_LOGIN_ACCOUNT")
@SQLDelete(sql = "update WWYT_BASE_LOGIN_ACCOUNT set DELETE_FLAG = '1' where id= ?")
@SQLDeleteAll(sql = "update WWYT_BASE_LOGIN_ACCOUNT set DELETE_FLAG = '1' where id= ?")
@Where(clause = "DELETE_FLAG = '0'")

public class BaseLoginAccount implements java.io.Serializable {

    // columns START
    // ID
    private java.lang.Long id;
    // 用户名
    private java.lang.String userName;
    // 密码
    private java.lang.String password;
    // 状态
    private java.lang.Integer state;
    // 机构编码
    private java.lang.String orgCode;
    // 创建人
    private java.lang.Long createBy;
    // 创建时间
    private java.util.Date createTime;
    // 更新人
    private java.lang.Long updateBy;
    // 更新时间
    private java.util.Date updateTime;
    // 删除标记
    private java.lang.String deleteFlag;
    // 扰码盐
    private java.lang.String salt;
    // columns END
    // 租户
    private java.lang.Long tenentId;

    private String role;

    //是否第一次登录 0 否; 1 是
    private Integer firstLogin;

    //登录历史  0 新账号从没登录; 1 登录过
    private Integer loginHistory;
    //菜单配置个性化
    private String menuIdJson;

    //0账号关闭,1账号开启
    private Integer showState;
    //app消息中心id
    private String clientId;

    @Column(name = "client_id")
    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    @Column(name = "show_state")
    public Integer getShowState() {
        return showState;
    }

    public void setShowState(Integer showState) {
        this.showState = showState;
    }

    public String getMenuIdJson() {
        return menuIdJson;
    }

    public void setMenuIdJson(String menuIdJson) {
        this.menuIdJson = menuIdJson;
    }

    @Column(name = "login_history")
    public Integer getLoginHistory() {
        return loginHistory;
    }

    public void setLoginHistory(Integer loginHistory) {
        this.loginHistory = loginHistory;
    }

    @Column(name = "first_login")
    public Integer getFirstLogin() {
        return firstLogin;
    }

    public void setFirstLogin(Integer firstLogin) {
        this.firstLogin = firstLogin;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "Id", unique = true, nullable = false)
    public java.lang.Long getId() {
        return this.id;
    }

    public void setId(java.lang.Long value) {
        this.id = value;
    }

    @Column(name = "USER_NAME", length = 50)
    public java.lang.String getUserName() {
        return this.userName;
    }

    public void setUserName(java.lang.String value) {
        this.userName = value;
    }

    @Column(name = "TENENT_ID")
    public java.lang.Long getTenentId() {
        return this.tenentId;
    }

    public void setTenentId(java.lang.Long value) {
        this.tenentId = value;
    }

    @Column(name = "PASSWORD", length = 128)
    public java.lang.String getPassword() {
        return this.password;
    }

    public void setPassword(java.lang.String value) {
        this.password = value;
    }

    @Column(name = "STATE")
    public java.lang.Integer getState() {
        return this.state;
    }

    public void setState(java.lang.Integer value) {
        this.state = value;
    }

    @Column(name = "ORG_CODE", length = 100)
    public java.lang.String getOrgCode() {
        return this.orgCode;
    }

    public void setOrgCode(java.lang.String value) {
        this.orgCode = value;
    }

    @org.springframework.data.annotation.CreatedBy
    @Column(name = "CREATE_BY")
    public java.lang.Long getCreateBy() {
        return this.createBy;
    }

    public void setCreateBy(java.lang.Long value) {
        this.createBy = value;
    }

    @org.springframework.data.annotation.CreatedDate
    @Column(name = "CREATE_TIME")
    public java.util.Date getCreateTime() {
        return this.createTime;
    }

    public void setCreateTime(java.util.Date value) {
        this.createTime = value;
    }

    @org.springframework.data.annotation.LastModifiedBy
    @Column(name = "UPDATE_BY")
    public java.lang.Long getUpdateBy() {
        return this.updateBy;
    }

    public void setUpdateBy(java.lang.Long value) {
        this.updateBy = value;
    }

    @Column(name = "UPDATE_TIME")
    public java.util.Date getUpdateTime() {
        return this.updateTime;
    }

    public void setUpdateTime(java.util.Date value) {
        this.updateTime = value;
    }

    @Column(name = "DELETE_FLAG", length = 2)
    public java.lang.String getDeleteFlag() {
        return this.deleteFlag;
    }

    public void setDeleteFlag(java.lang.String value) {
        this.deleteFlag = value;
    }

    @Column(name = "SALT", length = 20)
    public java.lang.String getSalt() {
        return this.salt;
    }

    public void setSalt(java.lang.String value) {
        this.salt = value;
    }

    @Column(name = "ROLE", length = 15)
    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    private BaseUser baseUser;

    @OneToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST})
    @JoinColumn(name = "USER_DETAIL_ID")
    public BaseUser getBaseUser() {
        return this.baseUser;
    }

    public void setBaseUser(BaseUser baseUser) {
        this.baseUser = baseUser;
    }

    private Set<BaseRole> roles = new LinkedHashSet<>(0);

    @JsonIgnore
    @ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST})
    @JoinTable(name = "WWYT_BASE_ROLE_USER_LOGIN_ROLE", joinColumns = @JoinColumn(name = "LOGIN_USER_ID"), inverseJoinColumns = @JoinColumn(name = "ROLE_ID"))
    public Set<BaseRole> getRoles() {
        return roles;
    }

    public void setRoles(Set<BaseRole> roles) {
        this.roles = roles;
    }

    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("id", getId())
                .append("userName", getUserName()).append("password", getPassword()).append("state", getState())
                .append("orgCode", getOrgCode()).append("createBy", getCreateBy()).append("createTime", getCreateTime())
                .append("updateBy", getUpdateBy()).append("updateTime", getUpdateTime())
                .append("deleteFlag", getDeleteFlag()).append("salt", getSalt()).append("showState",getShowState()).toString();
    }

    public int hashCode() {
        return new HashCodeBuilder().append(getId()).toHashCode();
    }

    public boolean equals(Object obj) {
        if (obj instanceof BaseLoginAccount == false)
            return false;
        if (this == obj)
            return true;
        BaseLoginAccount other = (BaseLoginAccount) obj;
        return new EqualsBuilder().append(getId(), other.getId()).isEquals();
    }

    @Transient
    public boolean isNormal() {
        return state != null && state == LoginUserStates.NORMAL.getCode();
    }

    @Transient
    public boolean isLocked() {
        return state != null && state == LoginUserStates.LOCKED.getCode();
    }

    @Transient
    public boolean isUnactivated() {
        return state == null || state == LoginUserStates.UNACTIVATED.getCode();
    }

}
